package com.atguigu.flink.state;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.AggregatingState;
import org.apache.flink.api.common.state.AggregatingStateDescriptor;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.api.common.state.ReducingStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2023/3/1
 *
 *  计算每种传感器的平均水位
 */
public class Demo8_KeyedAggregateState
{
    public static void main(String[] args) {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

                env
                   .socketTextStream("hadoop103", 8888)
                   .map(new WaterSensorMapFunction())
                   .keyBy(WaterSensor::getId)
                   .process(new KeyedProcessFunction<String, WaterSensor, String>()
                   {
                       //状态自带聚合功能
                       private AggregatingState<Integer, Double> avgVc;

                       @Override
                       public void open(Configuration parameters) throws Exception {

                           avgVc = getRuntimeContext().getAggregatingState(
                               new AggregatingStateDescriptor<>("avgVc", new AggregateFunction<Integer, MyAcc, Double>()
                               {
                                   @Override
                                   public MyAcc createAccumulator() {
                                       return new MyAcc(0, 0d);
                                   }

                                   @Override
                                   public MyAcc add(Integer value, MyAcc accumulator) {
                                       accumulator.count += 1;
                                       accumulator.sumVc += value;
                                       return accumulator;
                                   }

                                   @Override
                                   public Double getResult(MyAcc accumulator) {
                                       return accumulator.sumVc / accumulator.count;
                                   }

                                   //DataStream无需实现
                                   @Override
                                   public MyAcc merge(MyAcc a, MyAcc b) {
                                       return null;
                                   }
                               }, MyAcc.class));

                       }

                       @Override
                       public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {

                           //放入当前传感器的水位值，自动聚合
                           avgVc.add(value.getVc());

                           out.collect(ctx.getCurrentKey() + ":"+avgVc.get());
                       }
                   })
                   .print();

                try {
                            env.execute();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MyAcc {

        private Integer count;
        private Double sumVc;

    }
}
